package com.weiquan.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * 
 * @author weiquan
 * @since 2024-01-01
 */
@Slf4j
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置缓存
     */
    public void set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
        } catch (Exception e) {
            log.error("Redis设置缓存失败，key: {}, error: {}", key, e.getMessage());
        }
    }

    /**
     * 设置缓存并指定过期时间
     */
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, unit);
        } catch (Exception e) {
            log.error("Redis设置缓存失败，key: {}, error: {}", key, e.getMessage());
        }
    }

    /**
     * 设置缓存并指定过期时间（秒）
     */
    public void set(String key, Object value, long timeoutSeconds) {
        set(key, value, timeoutSeconds, TimeUnit.SECONDS);
    }

    /**
     * 获取缓存
     */
    public Object get(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error("Redis获取缓存失败，key: {}, error: {}", key, e.getMessage());
            return null;
        }
    }

    /**
     * 获取字符串缓存
     */
    public String getString(String key) {
        Object value = get(key);
        return value != null ? value.toString() : null;
    }

    /**
     * 删除缓存
     */
    public boolean delete(String key) {
        try {
            return Boolean.TRUE.equals(redisTemplate.delete(key));
        } catch (Exception e) {
            log.error("Redis删除缓存失败，key: {}, error: {}", key, e.getMessage());
            return false;
        }
    }

    /**
     * 判断key是否存在
     */
    public boolean hasKey(String key) {
        try {
            return Boolean.TRUE.equals(redisTemplate.hasKey(key));
        } catch (Exception e) {
            log.error("Redis判断key存在失败，key: {}, error: {}", key, e.getMessage());
            return false;
        }
    }

    /**
     * 设置过期时间
     */
    public boolean expire(String key, long timeout, TimeUnit unit) {
        try {
            return Boolean.TRUE.equals(redisTemplate.expire(key, timeout, unit));
        } catch (Exception e) {
            log.error("Redis设置过期时间失败，key: {}, error: {}", key, e.getMessage());
            return false;
        }
    }

    /**
     * 获取过期时间
     */
    public long getExpire(String key) {
        try {
            Long expire = redisTemplate.getExpire(key);
            return expire != null ? expire : -1;
        } catch (Exception e) {
            log.error("Redis获取过期时间失败，key: {}, error: {}", key, e.getMessage());
            return -1;
        }
    }

    /**
     * 递增
     */
    public long increment(String key) {
        try {
            Long result = redisTemplate.opsForValue().increment(key);
            return result != null ? result : 0;
        } catch (Exception e) {
            log.error("Redis递增失败，key: {}, error: {}", key, e.getMessage());
            return 0;
        }
    }

    /**
     * 递增指定值
     */
    public long increment(String key, long delta) {
        try {
            Long result = redisTemplate.opsForValue().increment(key, delta);
            return result != null ? result : 0;
        } catch (Exception e) {
            log.error("Redis递增失败，key: {}, delta: {}, error: {}", key, delta, e.getMessage());
            return 0;
        }
    }

    /**
     * 递减
     */
    public long decrement(String key) {
        try {
            Long result = redisTemplate.opsForValue().decrement(key);
            return result != null ? result : 0;
        } catch (Exception e) {
            log.error("Redis递减失败，key: {}, error: {}", key, e.getMessage());
            return 0;
        }
    }

    /**
     * 递减指定值
     */
    public long decrement(String key, long delta) {
        try {
            Long result = redisTemplate.opsForValue().decrement(key, delta);
            return result != null ? result : 0;
        } catch (Exception e) {
            log.error("Redis递减失败，key: {}, delta: {}, error: {}", key, delta, e.getMessage());
            return 0;
        }
    }

    /**
     * 删除缓存（别名方法）
     */
    public boolean del(String key) {
        return delete(key);
    }

    /**
     * 批量删除缓存
     */
    public long delete(String... keys) {
        try {
            Long result = redisTemplate.delete(Arrays.asList(keys));
            return result != null ? result : 0;
        } catch (Exception e) {
            log.error("Redis批量删除缓存失败，keys: {}, error: {}", Arrays.toString(keys), e.getMessage());
            return 0;
        }
    }
}
